package com.xxhui.bezierdemo.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PointF;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

/**
 * Created by hui on 2016/7/7.
 */
public class ThirdOrder extends View {

    private Paint paint ;
    private int centerX ,centerY ;
    private PointF start , end , control1 , control2 ;
    private boolean switchControl = true;
    private Path path;

    public ThirdOrder(Context context, AttributeSet attrs) {
        super(context, attrs);
        initResourse();
    }

    public ThirdOrder(Context context) {
        this(context , null);
    }

    private void initResourse(){
        paint = new Paint();
        paint.setColor(Color.BLACK);
        paint.setStrokeWidth(8);
        paint.setStyle(Paint.Style.STROKE);
        paint.setTextSize(60);
        start = new PointF(0,0);
        end = new PointF(0,0);
        control1 = new PointF(0,0);
        control2 = new PointF(0,0);
    }

    public void setSwitchControl(boolean b){
        this.switchControl = b;
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        centerX = w/2;
        centerY = h/2;

        start.x = centerX-200;
        start.y = centerY;
        end.x = centerX+200;
        end.y = centerY;
        control1.x = centerX-200 ;
        control1.y = centerY-100;
        control2.x = centerX+200;
        control2.y = centerY-100;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if(switchControl){
            control1.x = event.getX();
            control1.y = event.getY();
        }else{
            control2.x = event.getX();
            control2.y = event.getY();
        }
        invalidate();
        return true;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawPointF(canvas);
        drawLine(canvas);
        drawBezier(canvas);
    }

    private void drawPointF(Canvas canvas){
        paint.setColor(Color.GRAY);
        paint.setStrokeWidth(20);
        canvas.drawPoint(start.x , start.y , paint);
        canvas.drawPoint(end.x , end.y , paint);
        canvas.drawPoint(control1.x , control1.y , paint);
        canvas.drawPoint(control2.x , control2.y , paint);
    }

    private void drawLine(Canvas canvas){
        paint.setStrokeWidth(4);
        canvas.drawLine(start.x , start.y , control1.x , control1.y , paint);
        canvas.drawLine(control1.x ,control1.y , control2.x ,control2.y ,paint);
        canvas.drawLine(control2.x , control2.y , end.x , end.y  ,paint);
    }

    private void drawBezier(Canvas canvas){
        paint.setColor(Color.RED);
        paint.setStrokeWidth(8);

        path = new Path();
        path.moveTo(start.x , start.y);
        path.cubicTo(control1.x , control1.y ,control2.x ,control2.y , end.x , end.y);

        canvas.drawPath(path , paint);
    }
}
